package com.jt.assistant.utils;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;

import com.dothantech.lpapi.LPAPI;
import com.dothantech.printer.IDzPrinter;
import com.jt.assistant.bean.CartonGoods;
import com.jt.assistant.bean.CartonResponseBean;

public class B11PrinterHelper {

    public static boolean printBarcode(LPAPI mB11PrintApi, String jdCode, String cartonCode) {
        mB11PrintApi.startJob(48, 78, 0);
        mB11PrintApi.setItemOrientation(90);
        mB11PrintApi.drawText(String.format("JD单号：%s", jdCode), 43, 5, 0, 0, 5);
        mB11PrintApi.draw1DBarcode(cartonCode, LPAPI.BarcodeType.AUTO, 10, 14, 15, 50, 3);
        // 结束绘图任务提交打印
        Bundle bundle = new Bundle();
        bundle.putInt(IDzPrinter.PrintParamName.PRINT_DENSITY, 6);
        return mB11PrintApi.commitJobWithParam(bundle);
    }


    public static void B11PrintData(LPAPI mB11PrintApi, CartonResponseBean cartonGoods) {
        if (cartonGoods == null || cartonGoods.getCartonGoodsList() == null || cartonGoods.getCartonGoodsList().size() == 0) {
            return;
        }
        int goodsNum = cartonGoods.getCartonGoodsList().size();
        int pageSize = 0;
        if (goodsNum < 8) {
            pageSize = 1;
        } else if (goodsNum < 18) {
            pageSize = 2;
        } else if (goodsNum < 28) {
            pageSize = 3;
        } else if (goodsNum < 38) {
            pageSize = 4;
        } else if (goodsNum < 48) {
            pageSize = 5;
        } else if (goodsNum < 58) {
            pageSize = 6;
        }
        switch (pageSize) {
            case 1:
                preparePrintHead(mB11PrintApi,cartonGoods);
                preparePrintGoods(mB11PrintApi,cartonGoods, 1, pageSize);
                preparePrintBarcode(mB11PrintApi,cartonGoods);
                print(mB11PrintApi);
                break;
            case 2:
                preparePrintHead(mB11PrintApi, cartonGoods);
                preparePrintGoods(mB11PrintApi, cartonGoods, 1, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 2, pageSize);
                preparePrintBarcode(mB11PrintApi, cartonGoods);
                print(mB11PrintApi);
                break;
            case 3:
                preparePrintHead(mB11PrintApi, cartonGoods);
                preparePrintGoods(mB11PrintApi, cartonGoods, 1, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 2, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 3, pageSize);
                preparePrintBarcode(mB11PrintApi, cartonGoods);
                print(mB11PrintApi);
                break;
            case 4:
                preparePrintHead(mB11PrintApi, cartonGoods);
                preparePrintGoods(mB11PrintApi, cartonGoods, 1, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 2, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 3, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 4, pageSize);
                preparePrintBarcode(mB11PrintApi, cartonGoods);
                print(mB11PrintApi);
                break;
            case 5:
                preparePrintHead(mB11PrintApi, cartonGoods);
                preparePrintGoods(mB11PrintApi, cartonGoods, 1, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 2, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 3, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 4, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 5, pageSize);
                preparePrintBarcode(mB11PrintApi, cartonGoods);
                print(mB11PrintApi);
                break;
            case 6:
                preparePrintHead(mB11PrintApi, cartonGoods);
                preparePrintGoods(mB11PrintApi, cartonGoods, 1, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 2, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 3, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 4, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 5, pageSize);
                preparePrintGoods(mB11PrintApi, cartonGoods, 6, pageSize);
                preparePrintBarcode(mB11PrintApi, cartonGoods);
                print(mB11PrintApi);
                break;
        }
    }


    @SuppressLint("DefaultLocale")
    public static void preparePrintHead(LPAPI mB11PrintApi, CartonResponseBean cartonGoods) {
        mB11PrintApi.startJob(48, 78, 0);
        mB11PrintApi.drawText(String.format("%s 第%d箱", (!TextUtils.isEmpty(cartonGoods.getReturnsCode()) ?
                        cartonGoods.getReturnsCode() : cartonGoods.getChannelOrderNo()),
                Integer.parseInt(cartonGoods.getCartonCode().substring(cartonGoods.getCartonCode().length() - 6))), 4, 3, 0, 0, 5);
        String customerName = cartonGoods.getCustomerName();
        String storeName = cartonGoods.getStoreName();
        if (!TextUtils.isEmpty(customerName) && customerName.length() > 10) {
            customerName = customerName.substring(0, 10);
        }
        if (!TextUtils.isEmpty(storeName) && storeName.length() > 10) {
            storeName = storeName.substring(0, 10);
        }
        if (!TextUtils.isEmpty(customerName)) {
            mB11PrintApi.drawText(customerName, 4, 10, 0, 0, 4);
        }
        if (!TextUtils.isEmpty(storeName)) {
            mB11PrintApi.drawText(storeName, 4, 15, 0, 0, 4);
        }
    }


    //default 20
    @SuppressLint("DefaultLocale")
    public static void preparePrintGoods(LPAPI mB11PrintApi, CartonResponseBean cartonGoods, int pageIndex, int totalPage) {
        int beginY = 20;
        int beginI = 0;
        int endI = 0;
        boolean needPrint = false;
        int goodsNum = cartonGoods.getCartonGoodsList().size();
        if (pageIndex == 1 && totalPage == pageIndex) {//只有一页
            beginI = 0;
            endI = goodsNum;
        } else if (pageIndex == 1 && pageIndex < totalPage) {//多页的第一页
            beginI = 0;
            endI = 10;
            if (goodsNum < endI) {
                endI = goodsNum;
            }
            needPrint = true;
        } else if (totalPage > 2 && pageIndex + 1 == totalPage) {//三页和以上,到第二页
            beginI = 10 + (pageIndex - 2) * 10;
            endI = 10 + (pageIndex - 1) * 10;
            if (goodsNum < endI) {
                endI = goodsNum;
            }
            beginY = 5;
            needPrint = true;
        } else if (totalPage > 2 && pageIndex + 1 < totalPage) {//三页和以上,到第三页
            beginI = (pageIndex - 1) * 10;
            endI = pageIndex * 10;
            needPrint = true;
            beginY = 5;
        } else if (pageIndex > 1 && pageIndex == totalPage) {
            beginI = Math.min(goodsNum, (totalPage - 1) * 10);
            endI = goodsNum;
            beginY = 5;
        }

        if (beginI < endI) {
            mB11PrintApi.drawText("商品列表：（SKU/数量）", 4, beginY, 0, 0, 4);
        } else {
            mB11PrintApi.drawText(String.format("%s 第%d箱", cartonGoods.getChannelOrderNo(), Integer.parseInt(cartonGoods.getCartonCode().substring(12))), 4, 3, 0, 0, 5);
        }
        for (int i = beginI; i < endI; i++) {
            CartonGoods goods = cartonGoods.getCartonGoodsList().get(i);
            mB11PrintApi.drawText(goods.getSku(), 4, beginY + 5 * (i - beginI + 1), 0, 0, 4);
            mB11PrintApi.drawText(goods.getNum() + "", 34, beginY + 5 * (i - beginI + 1), 0, 0, 4);
        }
        if (needPrint) {
            print(mB11PrintApi);
        }
    }

    public static void preparePrintBarcode(LPAPI mB11PrintApi, CartonResponseBean cartonGoods) {
        mB11PrintApi.draw1DBarcode(cartonGoods.getCartonCode(), LPAPI.BarcodeType.AUTO, 4, 60, 40, 15, 3);
    }

    public static void print(LPAPI mB11PrintApi) {
        Bundle bundle = new Bundle();
        bundle.putInt(IDzPrinter.PrintParamName.PRINT_DENSITY, 6);
        mB11PrintApi.commitJobWithParam(bundle);
    }
}
